package com.zbcbms.controller;

import com.alibaba.druid.util.StringUtils;
import com.zbcbms.common.AlertUtilCommon;
import com.zbcbms.entity.User;
import com.zbcbms.entity.tableData.UserTableData;
import com.zbcbms.service.impl.UserServiceImpl;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;

import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author fengq
 */
public class UpdateUserController implements Initializable {
    @FXML
    private TextField updateUsername;
    @FXML
    private TextField updateUserNumber;
    @FXML
    private DatePicker updateJoinTime;
    @FXML
    private TextField updatePassword;
    @FXML
    private Button updateUserBtn;
    @FXML
    private Button resetUserBtn;
    @FXML
    private AnchorPane updateUserAnchorPane;
    @FXML
    private ComboBox<String> updateRole;

    private AlertUtilCommon alertUtilCommon=new AlertUtilCommon();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        List<String> snackList = Arrays.asList("系统管理员", "图书管理员","借阅者");
        ObservableList<String> obList = FXCollections.observableArrayList(snackList);
        updateRole.setItems(obList);
    }

    @FXML
    public void updateUserAction(){
        String username=updateUsername.getText();
        String userNumber=updateUserNumber.getText();
        String joinTime = updateJoinTime.getEditor().getText();
        String password=updatePassword.getText();
        String role=updateRole.getValue();
        if (StringUtils.isEmpty(username)||StringUtils.isEmpty(userNumber)||StringUtils.isEmpty(joinTime)||StringUtils.isEmpty(password)){
            alertUtilCommon.AlterMessageInform(updateUsername.getScene().getWindow(),Alert.AlertType.ERROR,"错误提醒","参数不能为空","参数为空警告");
        }else {
            try {
                Date date = new SimpleDateFormat("yyyy-MM-dd").parse(joinTime);
                User user = new User();
                user.setUserName(username);
                user.setPassword(password);
                user.setUserNumber(userNumber);
                user.setJoinDate(date);
                user.setRole(role);
                Boolean aBoolean = new UserServiceImpl().updateById(user);
                if (aBoolean){
                    alertUtilCommon.AlterMessageInform(updateUsername.getScene().getWindow(),Alert.AlertType.CONFIRMATION,"修改提示","修改成功","成功提醒");
                    AnchorPane parent = (AnchorPane) updateUserAnchorPane.getParent();
                    HomePageController homePageController = new HomePageController();
                    homePageController.setMainFrameAnchorPane(parent);
                    homePageController.do_queryUserMenuItem_event(null);
                }else {
                    alertUtilCommon.AlterMessageInform(updateUsername.getScene().getWindow(),Alert.AlertType.ERROR,"错误提醒","修改失败","操作失败");
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }

    @FXML
    public void resetUserAction(){
        updateUsername.setText("");
        updateUserNumber.setText("");
        updateJoinTime.getEditor().setText("");
        updatePassword.setText("");
        updateRole.getEditor().setText("");
    }

    public void UserMessageInit(UserTableData userTableData){
        String password = new UserServiceImpl().getPasswordByNumber(userTableData.getUserNumber().get());
        updateUserNumber.setText(userTableData.getUserNumber().get());
        updateUsername.setText(userTableData.getUserName().get());
        updateJoinTime.getEditor().setText(userTableData.getJoinDate().get());
        updatePassword.setText(password);
        updateRole.setValue(userTableData.getRole().get());
    }


}
